﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace XamlFilter
{
    class Program
    {
        static int Main(string[] args)
        {
            var argAnalyzer = new ArgumentAnalyzer(Environment.CommandLine, '/', false);
            var settings = new Settings();
            try
            {
                argAnalyzer.FillObject(settings);
            }
            catch (Exception)
            {
                DisplayUsage();
                return 1;
            }

            if (!File.Exists(settings.Template))
            {
                Console.WriteLine("Specified template file '{0}' is not found.", settings.Template);
                Console.WriteLine();

                DisplayUsage();
                return 1;
            }

            try
            {
                var generator = new Generator(settings.Template);
                generator.Conditions = settings.Conditions;
                generator.Indent = !settings.NoIndent;
                generator.Generate(settings.Output);
                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

                return 1;
            }
        }

        static void DisplayUsage()
        {
            Console.WriteLine(@"Usage:
    XamlFilter /template:path\to\template.xaml /out:path\to\output.xaml [/indent[+|-]] [/condition:sl [/condition:black]]");
        }

        public class Settings
        {
            [NamedArgument("template")]
            public string Template { get; set; }
            [NamedArgument("out")]
            public string Output { get; set; }
            [NamedArgument("condition",MaxRequired = int.MaxValue)]
            public string[] Conditions { get; set; }
            [NamedArgument("noindent")]
            public bool NoIndent { get; set; }
        }
    }
}
